import React, { useEffect, useState } from 'react';
import type { FormProps } from 'antd';
import { useAppDispatch,useAppSelector } from '@/store/reactreduxts';
import * as asycnfn from '../../store/crmsSlice'

type FieldType = {
  username?: string;
  password?: string;
  remember?: string;
};

import {
  Button,
  Cascader,
  DatePicker,
  Form,
  Input,
  InputNumber,
  Radio,
  Select,
  Switch,
  TreeSelect,
} from 'antd';






const Myform: React.FC = (props) => {
    const [form]=Form.useForm();
    useEffect(()=>{
        form.setFieldsValue(props.itemobj)
    },[])
    console.log(props.itemobj);
    
//获取状态
let obj=useAppSelector(state=>state.todos)
let dispatch=useAppDispatch()
console.log(obj);

const onFinish: FormProps<FieldType>['onFinish'] = (values) => {
if(props.title=='添加'){
  console.log('Success:', values);
//添加对象
let obj={
    ...values,
    id:new Date().getTime(),
    time:new Date().getTime()
}  
//执行添加
dispatch(asycnfn.async_addshoplist(obj))    
}else{
//编辑
let obj={
    ...values,
    id:props.itemobj.id,
    time:new Date().getTime()
} 
//执行编辑
dispatch(asycnfn.async_editshoplist(obj))
}


props.onCancel()
};

 

  return (
    <Form onFinish={onFinish} form={form}>
      <Form.Item label="商品编号" name="bianhao"
        rules={[{ required: true, message: '请输入!' }]}>
        <Input />
      </Form.Item>
      <Form.Item label="商品编码" name="bianma"
      rules={[{ required: true, message: '请输入!' }]}>
        <Input />
      </Form.Item>
       <Form.Item label="商品名称" name="name"
       rules={[{ required: true, message: '请输入!' }]}>
        <Input />
      </Form.Item>
      <Form.Item label="商品排序" name="sort"
      rules={[{ required: true, message: '请输入!' }]}>
        <InputNumber />
      </Form.Item>
      <Form.Item label="状态" name="zhuangtai">
        <Switch />
      </Form.Item>
        <Button type='primary' htmlType='submit'>完成</Button>
    </Form>
  );
};

export default Myform;